import { useLocation, useNavigate } from 'react-router-dom';
import useGetUserInfo from './useGetUserInfo';
import {
  LOGIN_PATHNAME,
  MANAGE_INDEX_PATHNAME,
  isLoginOrRegisterPage,
  isNotNeedUserInfo,
} from '../router';
import { useEffect } from 'react';
function useNavPage(waitingUserData: boolean) {
  const { pathname } = useLocation();
  const { username } = useGetUserInfo();
  console.log('useNavPage pathname', pathname);
  console.log('useNavPage username', username);
  const nav = useNavigate();
  useEffect(() => {
    if (waitingUserData) {
      return;
    }
    console.log('useNavPage', username);
    //已经登录
    if (username) {
      if (isLoginOrRegisterPage(pathname)) {
        nav(MANAGE_INDEX_PATHNAME);
      }
      return;
    }
    //未登录，不需要登录页面
    if (isNotNeedUserInfo(pathname)) {
      return;
    }
    //未登录，需要登录页面
    nav(LOGIN_PATHNAME);
  }, [waitingUserData, username, pathname]);
}

export default useNavPage;
